import es

__all__ = ["Extension", "addons"]

class ExtensionManager(object):
    """
    This object is used to manage the extensions of wcs and ensure that we give
    each extension the ability to have an API to interact with the main wcs base
    This object stores all the individual extension objects and provide a 
    key-value like container to access the individual extension interface.
    """
    def __init__(self):
        """
        Default constructor, initialise the container.
        """
        self.extDict = {}

    def __getitem__(self, name):
        """
        Returns an extension instance depending on the name of

        @param str name The name of the Extension
        @retrun Extension The instance created by the extension name.
        """
        return self.extDict[name]
        
    def __delitem__(self, name):
        """
        Executed when an extension object as had it's attribute removed. Assume 
        we want to unload the extension.
        
        @param str name The name of the extension to remove
        """
        self.removeExtension(name)
            
    def removeExtension(self, name):
        """
        Executed when an extension object as had it's attribute removed. Assume 
        we want to unload the extension.
        
        @param str name The name of the extension to remove
        """
        if name in self.extDict:
            del self.extDict[name]
            es.unload("wcs/extension_folder/%s" % name)
        
    def getExtension(self, name):
        """
        Returns an extension instance depending on the name of

        @param str name The name of the extension
        @retrun Extension The instance created by the extension name.
        """
        return self.extDict[name]

    def loadExtension(self, name):
        """
        Loads an extension based upon an extension name and returns the 
        extension created by the name.

        @param str name The name of the extension
        @return Extension The ninstance created by the extension name.
        """ 
        self.extDict[name] = Extension(name)
        es.load("wcs/extension_folder/%s" % name)
        return self.extDict[name]

class Extension(object):
    """
    This object is to be instanted for every extension that is loaded. It will
    store all relevant information about a specific extension and provide an
    interface to interact with WCS as a whole.
    """
    def __init__(self, extensionName):
        """
        Default constructor, initialises all the object's settings.

        @param str extensionName The name of the extension this represents
        """
        self.name = extensionName
        
addons = ExtensionManager()